﻿$.fn.digits = function () {
    return this.each(function () {
        $(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });
};

function setHeightLeftMenu() {
    var _h = $(window).height();
    $('#left').css("height", _h);
}

function CheckFileUpload(cmd) {
    // input file change
    cmd.click(function () {
        var flag = true;
        $('input[type="file"]').each(function () {
            var val = $(this).val();
            if (val != null && val != '') {
                var extension = val.substring(val.lastIndexOf('.'), val.length);
                switch (extension.toLowerCase()) {
                    case '.png':
                    case '.jpg':
                    case '.gif':
                    case '.icon':
                    case '.jpeg': break;
                    default: alert('Bạn phải nhập ảnh theo định dang đuôi .jpg, .jpeg, .png, .gif, .icon !');
                        flag = false;
                }
            }
        });
        return flag;
    });
}

$(function () {
    $('#btnLogout').click(function () {
        $.ajax({
            url: '/AdminLogin/Logout',
            data: { },
            success: function (data) {
                if (data ) {
                    location.href = '/';
                }
            },
            error: function () {
                alert('Đã có lỗi xảy ra!');
            }
        });
    });

    // Hide show quick menu ------------------------------------
    $('ul.quick-menu li').click(function () {
        var index = $(this).index();
        $('div.box-Info').hide();
        $('div.box-Info').eq(index).show();
        if ($(this).hasClass('sub-template')) {
            LoadTemplateOfCate();
        }
    });
    /*
    Check Box Del On grid
    */
    $('table.table-odd input[name$=chkAll]').click(function () {
        var chk = $(this).attr('checked');
        var cmdItem = $('table.table-odd input[name$=chkItem]');
        if (chk == '' || chk == undefined) {
            cmdItem.attr('checked', false);
        } else {
            cmdItem.attr('checked', true);
        }
    });

    $('table.goldenTimeTable2 input[name$=chkAll]').click(function () {
        var chk = $(this).attr('checked');
        var cmdItem = $('table.goldenTimeTable2 input[name$=chkItem]');
        if (chk == '' || chk == undefined) {
            cmdItem.attr('checked', false);
        } else {
            cmdItem.attr('checked', true);
        }
    });

    /*
    Hoi truoc khi xoa
    */

    //$('.table-odd .icon-del').click(function () {
    //    if (!confirm("Bạn chắc chắn muốn xóa bản ghi này ?")) {
    //        return false;
    //    }
    //});
});

function showAllcheckBoxGoldenTime() {
    $('table.plcProductGold input[name$=chkAll]').click(function () {
        alert('OK')
        var chk = $(this).attr('checked');
        var cmdItem = $('table.plcProductGold input[name$=chkItem]');
        if (chk == '' || chk == undefined) {
            cmdItem.attr('checked', false);
        } else {
            cmdItem.attr('checked', true);
        }
    });
}

/* Submit form use a tag on toolbox */
function SubmitFormUseToolBox(objClick, objForm) {
    objClick.click(function () {
        objForm.submit();
    });
}

function toSplitNumberByComma(ctr) {
    var val = ctr.val();
    if (val != null && val != '') ctr.val(val.split(',')[0]);
    ctr.mask('000.000.000.000.000', { reverse: true });
}

function RemoveSpecialCharacter(str) {
    str = str.replace(/[^a-zA-Z0-9]/g, '');
    return str;
}

function ShowLoading() {
    $('#ajaxBusy').fadeIn();
}

function HideLoading() {
    $('#ajaxBusy').fadeOut();
}

//dem tu trong AddNews
function countTextAreaChar(txtarea, l, place) {
    var plc = $('input[name=' + place + "]");
    var len = $(txtarea).val().length;
    if (len > l) $(txtarea).val($(txtarea).val().slice(0, l));
    else plc.val(len);
}

$('.icon-close-popup').live("click", function () {
    $('.popup-wap').fadeOut();
    $('.popup-wap').remove();
});

/*
    Get time milisecond -----------------------------
*/
function GetTimeGlobal() {
    var currentDate = new Date();
    return currentDate.getTime();
}